<div id="app">
    <h1>采集设置</h1>
    <div class="form-item">
        <label for="category_links">
            选择栏目
        </label>
        <select name="category_links" @change="get_page" id="category_links" v-model="category">
            <option v-for="item in category_links" :value="item.link">{{item.title}}</option>
        </select>
        <span class="info">
            选择的地址: <code><a target="_blank" :href="category">{{category}}</a></code>
        </span>
    </div>
    <div class="form-item">
        <label for="paged">
            采集页数
        </label>
        <select name="paged" id="paged" v-model="paged">
            <option v-for="item in pages" :value="item" :key="item">{{item}}页</option>
        </select>
        <span class="info">当前栏目总页数 {{pages}}</span>
    </div>
    <div class="form-item">
        <label for="term">
            选择WP分类
        </label>
        <select name="term" id="term" v-model="term" required>
            <option>请选择</option>
            <option v-for="item in terms" :value="item.slug">{{item.name}}</option>
        </select>
    </div>
    <div class="form-item d-flex">
        <div class="state">
            {{status}}
        </div>
        <span class="app-list">
            <p v-for="(val,key) in list" :key="key"><b><a :href="val.url">{{(key+1) + ': '+val.title}}</a></b></p>
        </span>
    </div>
    <div class="form-item">
        <button class="start-btn" @click="run">开始采集</button>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.11"></script>
<script>
    const AJAX_URL = "<?php echo admin_url('admin-ajax.php');?>"
    const app = new Vue({
        el: '#app',
        data() {
            return {
                category: 'https://www.onlinedown.net/sort/194/',
                category_links: [
                    {
                        title: '安卓游戏',
                        link: 'https://www.onlinedown.net/sort/194/'
                    },
                    {
                        title: '安卓软件',
                        link: 'https://www.onlinedown.net/sort/android/'
                    },
                    {
                        title: '苹果游戏',
                        link: 'https://www.onlinedown.net/sort/249/'
                    },
                    {
                        title: '苹果软件',
                        link: 'https://www.onlinedown.net/sort/iphone/'
                    }
                ],
                paged: 1,
                pages: 10,
                term: '',
                terms: [],
                status: '等待运行',
                list: [],
            }
        },
        created() {
           this.get_terms()
           this.get_page()
        },
        methods:{
            async get_terms() {
                const {data} = await this.request({
                    action: 'get_term'
                });
                this.terms = data;
            },
            async get_page() {
                this.status = '获取页数中...'
                const {data} = await this.request({
                    action: 'get_pages',
                    url: this.category
                },'POST');
                this.pages = Number(data.replace(/[^0-9]/ig, ''));
                this.status = '等待运行'
            },
            async run() {
                if (this.status === '正在运行...') {return false;}
                if (!this.term) {
                    alert("请选择WP分类");
                    return false;
                }
               this.status = '正在运行...';

               const {data, list} = await this.request({
                   action: 'run',
                   term_id: this.term,
                   paged: this.paged,
                   category_link: this.category,
               },'POST');
                this.status = `运行完毕!`;

                setTimeout(() => {
                    this.status = '等待运行';
                },3000)
                this.list = list;
            },
            request(data,type = 'GET') {
                return new Promise((resolve,reject) => {
                    jQuery.ajax({
                        url: AJAX_URL,
                        data,
                        type,
                        success: res => {
                            resolve(res)
                        },
                        fail: err => reject(err)
                    })
                })
            }
        }
    })
</script>
<style>
    .form-item {
        margin-top: 10px;
    }
    .d-flex {
        display: flex !important;
    }
    .app-list {
        margin-left: 2em;
        display: grid;
        grid-template-columns: repeat(4,1fr);
        column-gap: 20px;
    }
    .form-item label {
        display: inline-block;
        vertical-align: top;
        width: 120px;
    }
    .form-item .info {
        margin-left: 2em;
    }
    .form-item select {
        width: 150px;
    }
    .start-btn {
        padding: 0.2em 2.5em;
        cursor: pointer;
    }
    .state {
        display: inline-block;
        vertical-align: top;
        width: 275px;
        flex: none;
        height: 200px;
        font-size: 1.25em;
        text-align: center;
        line-height: 200px;
        color: #fff;
        background: grey;
    }
</style>